package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.RolePermissionDTO;
import top.zywork.query.RoleSysMenuQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.RoleSysMenuService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.RolePermissionVO;
import top.zywork.vo.RoleSysMenuVO;

import java.util.List;

/**
 * RoleSysMenuController控制器类<br/>
 *
 * 创建于2019-09-25<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/role-sys-menu")
@Slf4j
public class RoleSysMenuController extends BaseController {

    private RoleSysMenuService roleSysMenuService;

    /**
     * 批量保存用户菜单权限
     * @param roleSysMenuVOList
     * @param bindingResult
     * @return
     */
    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存角色与菜单配置")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<RoleSysMenuVO> roleSysMenuVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        roleSysMenuService.saveBatch(BeanUtils.copyListObj(roleSysMenuVOList, RoleSysMenuVO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/multi/{id}")
    public ResponseStatusVO listById(@PathVariable("id") Long id) {
        PagerDTO pagerDTO = roleSysMenuService.listById(id);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), RoleSysMenuVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = roleSysMenuService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), RoleSysMenuVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody RoleSysMenuQuery roleSysMenuQuery) {
        PagerDTO pagerDTO = roleSysMenuService.listAllByCondition(roleSysMenuQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), RoleSysMenuVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody RoleSysMenuQuery roleSysMenuQuery) {
        PagerDTO pagerDTO = roleSysMenuService.listPageByCondition(roleSysMenuQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), RoleSysMenuVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 查询用户角色
     * @return
     */
    @GetMapping("admin/all-menu")
    public ResponseStatusVO listAllUserMenu() {
        return roleSysMenuService.listAllByRoleIdArr(SecurityUtils.getJwtUser().getUserId());
    }

    /**
     * 查询指定角色菜单权限
     * @param roleId
     * @return
     */
    @GetMapping("admin/role-menu/{roleId}")
    public ResponseStatusVO listMenuDataByRoleId(@PathVariable("roleId") Long roleId) {
        RoleSysMenuQuery roleSysMenuQuery = new RoleSysMenuQuery();
        roleSysMenuQuery.setRoleId(roleId);
        roleSysMenuQuery.setRoleMenuIsActive((byte)0);
        roleSysMenuQuery.setSortColumn("t_sys_menu.menu_level, t_sys_menu.menu_order");
        roleSysMenuQuery.setSortOrder("asc");
        return listAllByCondition(roleSysMenuQuery);
    }

    @Autowired
    public void setRoleSysMenuService(RoleSysMenuService roleSysMenuService) {
        this.roleSysMenuService = roleSysMenuService;
    }
}
